//
//  UserMode.swift
//  SinaWb
//
//  Created by Jiang on 16/5/12.
//  Copyright © 2016年 Jiang. All rights reserved.
//

import UIKit
let path  = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! )
let fileName = "/account.plist"
class UserModel: NSObject,NSCoding{
    
    //id 和 昵称
    var  id	:AnyObject?
    var idstr :	String?
    var screen_name	:String?
    var name: String?
    //地址
    var province :AnyObject?
    var city: AnyObject?
    var location:String?
    ///自我描述 和 博客地址
    var descriptions :String?
    var url :String?
    ///头像
    var profile_image_url:String?
    var avatar_large:String?
    var avatar_hd:String?
    var avatarURL:NSURL?{
        return NSURL(string: avatar_large ?? " ")
    }
        /// 会员等级，取值范围(1-6)
    var mbrank : Int = -1
    //expires_in
    var expires_in : AnyObject?{
        didSet{  expires_date = NSDate(timeIntervalSinceNow: expires_in as! NSTimeInterval)  }
    }
    var expires_date :NSDate?
    //令牌
    var access_token :String?
    var uid :AnyObject?
    
    ///认证类型
    var verified:AnyObject?
    ///  用户认证类型 -1 : 没有认证 , 1: 认证用户 , 2，3，5 :  企业认证, 220,0: 达人
    var verified_type: Int = -1
    var profile_url:String?
    var domain: String?
    var weihao :String?
    var gender: String?
    var followers_count	:AnyObject?
    var friends_count:AnyObject?
    var statuses_count:AnyObject?
    var favourites_count:AnyObject?
    var created_at:String?
    var following:AnyObject?
    var allow_all_act_msg:AnyObject?
    var geo_enabled:AnyObject?

    var remark:String?
    var status:AnyObject?
    var allow_all_comment:AnyObject?

    var verified_reason:String?
    var follow_me:AnyObject?
    var online_status:AnyObject?
    var bi_followers_count:AnyObject?
    var  lang:String?
    var OringDic : Dictionary<String,AnyObject>?
    override init() {
        super.init()
    }
    init(dictionary : Dictionary<String,AnyObject>){
        super.init()
        OringDic = dictionary
        setValuesForKeysWithDictionary(dictionary)

    }
    
    //1.获取acess_token时，合并acess_token字典和用户字典
    //2.使用单例时
    func setProperyWithDict(dict : [String : AnyObject]) -> UserModel{
        var dic = dict
        if OringDic != nil {
            for item in OringDic!{
                dic.updateValue(item.1, forKey: item.0)
            }
        }
        OringDic  = dic
        
        setValuesForKeysWithDictionary(dict)
        return self
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {

        //不移除会影响mode的description显示,但不会影响解档和归档
        OringDic?.removeAtIndex((OringDic?.indexForKey(key))!)
    }
    
    //MARK: - 解档
    class func GetTheUserMode()->UserModel?{
        guard let a  = NSKeyedUnarchiver.unarchiveObjectWithFile(path + fileName) as? UserModel else{
            return nil
        }
        return a
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        
        //OringDic只是一个保存key的容器，方便用用解档
        let  dic: Dictionary? = NSKeyedUnarchiver.unarchiveObjectWithFile(path + "/OringDic.plist")  as? [String : AnyObject]
        
        guard dic != nil else{
            return
        }
       
        OringDic = dic!

        for item in dic! {
            
            setValue(aDecoder.decodeObjectForKey(item.0 )!, forKey: item.0)

        }


        
    }
    //MARK: - 归档
    func SaveUserMode(){
        
        NSKeyedArchiver.archiveRootObject(self, toFile: path + fileName)
        let dic = NSDictionary(dictionary: OringDic!)
        NSKeyedArchiver.archiveRootObject(dic, toFile: path + "/OringDic.plist")
        
        
    }
    func encodeWithCoder(aCoder: NSCoder) {
        for item in OringDic! {
            aCoder.encodeObject(item.1, forKey: item.0)
        }
    }
    
    //MARK: -
    override var description: String{
         print("__________\n\n")
 
        guard OringDic != nil else{  return "UserMode的数据 为空" }
        
        for a in OringDic! { print("    ",a.0," = ",a.1) }
        
        print("__________\n\n")
        
        return " "
    }

}
